package com.stud.common.jdbc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author Stud
 * @date 2023-12-06 14:48
 */

public class SqlHelp {
    private StringBuilder sql;

    private List<Object> params;

    private SqlHelp() {
    }

    public static SqlHelp init() {
        SqlHelp sqlHelp = new SqlHelp();
        sqlHelp.sql = new StringBuilder();
        sqlHelp.params = new ArrayList<>();
        return sqlHelp;
    }

    public static SqlHelp init(String sql, Object... params) {
        SqlHelp sqlHelp = init();
        sqlHelp.sql.append(sql);
        if (params != null && params.length != 0) {
            sqlHelp.params.addAll(Arrays.asList(params));
        }
        return sqlHelp;
    }

    public StringBuilder append(String s) {
        this.sql.append(s);
        return this.sql;
    }

    public void append(String s, Object... params) {
        this.sql.append(s);
        if (Objects.nonNull(params)) {
            this.params.addAll(Arrays.asList(params));
        }
    }

    public String getSql() {
        return sql.toString();
    }

    public Object[] getParams() {
//        return CollectionUtils.isEmpty(params) ? null : params.toArray();
        return params.toArray();
    }

    public void removeLastCode() {
        sql.setLength(sql.length() - 1);

    }

    public void clear() {
        sql.setLength(0);
        params.clear();
    }
}
